<!DOCTYPE html>
<?php
include('_includes/lock.php');
include ('_class/Navigator.php');
include ('_class/DataBaseConnector.php');

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $db = new DataBaseConnector();
    $db->connect();
    $parent = addslashes($_POST['parent']);
    $title = addslashes($_POST['title']);
    $bodytext = addslashes($_POST['bodytext']);
    if (isset($_POST['root']) && $_POST['root'] == "isRoot") {
        $isRoot = 1;
    } else {
        $isRoot = 0;
    }
    $user = $_SESSION['logged_user'];
    $db->insertData("articles", "NULL,'$title','$bodytext',CURDATE(),false,'$isRoot','$user', $parent");
    $lastID = mysql_insert_id();
    header("location: entry.php?id=$lastID");
}

function verifyParent() {
    if (isset($_GET['id'])) {
        $id = $_GET['id'];
        return "<input name=\"parent\" id=\"parent\" type=\"text\" value=\"$id\" hidden=\"true\"/><br/>";
    } else {
        return "<input name=\"parent\" id=\"parent\" type=\"text\" value=\"NULL\" hidden=\"true\"/><br/>";
    }
}
?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="resources/styles.css" type="text/css" media="screen" />
        <title>New Entry</title>
    </head>
    <body>
        <div id="wrapper">
            <header></header>
            <?php include ('_includes/nav.php') ?>
            <div id="page">
                <div id="content">
                    <form method="post">
                        <?php echo verifyParent(); ?>
                        <label for="title">Title:</label><br/>
                        <input name="title" id="title" type="text" maxlength="150" class="large text"/><br/>
                        <label for="bodytext">Body Text:</label><br/>
                        <textarea name="bodytext" id="bodytext" rows="10" cols="70"></textarea><br/>
                        <input type="checkbox" name="root" id="root" value="isRoot">Root Article<br>
                        <input type="submit" value="Create" class="medium blue button"/>
                    </form>
                </div>
                <?php include ('_includes/sidebar.php') ?>
            </div>
            <footer>
                <section>
                    <p>Copyright ©2012 - All Rights Reserved</p>
                </section>
            </footer>
        </div>
    </body>
</html>
